#include <unordered_map>
// 一个简单的并查集模板，只在查找时压缩路径

template <typename T>
class UnionFindSet
{
public:
    UnionFindSet()
    {
    }

    template <typename TContainer>
    UnionFindSet(const TContainer &container)
    {
        auto begin = container.begin();
        auto end = container.end();
        while (begin != end)
        {
            m[*begin] = *begin;
            ++begin;
            ++_groupCount;
        }
    }

    void insert(const T &value)
    {
        if (!m.count(value))
        {
            m[value] = value;
            ++_groupCount;
        }
    }

    bool exist(const T &value)
    {
        return m.count(value);
    }

    T find(const T &value)
    {
        if (m[value] == value)
        {
            return value;
        }
        return m[value] = find(m[value]);
    }

    void join(const T &value1, const T &value2)
    {
        if (!m.count(value1))
        {
            m[value1] = value1;
            ++_groupCount;
        }
        if (!m.count(value2))
        {
            m[value2] = value2;
            ++_groupCount;
        }
        if (find(value1) != find(value2))
        {
            m[find(value1)] = find(value2);
            --_groupCount;
        }
    }

    int groupCount()
    {
        return _groupCount;
    }

private:
    std::unordered_map<T, T> m;
    int _groupCount = 0;
};

class Solution
{
public:
    vector<bool> areConnected(int n, int threshold, vector<vector<int>> &queries)
    {
        UnionFindSet<int> s;
        for (int i = 1; i <= n; ++i)
        {
            s.insert(i);
        }
        vector<bool> visited(n + 1, false);
        for (int factorBegin = threshold + 1; factorBegin <= n; ++factorBegin)
        {
            if (visited[factorBegin])
            {
                continue;
            }

            for (int factor = factorBegin; factor <= n; factor += factorBegin)
            {
                s.join(factor, factorBegin);
            }

            visited[factorBegin] = true;
        }
        vector<bool> answer;
        for (auto &query : queries)
        {
            int a = query[0];
            int b = query[1];
            answer.push_back(s.find(a) == s.find(b));
        }
        return answer;
    }
};